/**
 * 初始化 BootStrap Table 的封装
 *
 * 约定：toolbar的id为 (bstableId + "Toolbar")
 *
 * @author fengshuonan
 */
(function () {


    var table = null;
    var LYTable = function (bstableId, url, columns) {
        var bgh = 500;
        try {
            var position = $('#offHight').offset();
            var top = position.top;
            bgh = $(".gray-bg").height() - top - 50;
        } catch (e) {

        }
        this.btInstance = null;					//jquery和BootStrapTable绑定的对象
        this.bstableId = bstableId;
        this.url = Feng.ctxPath + url;
        this.method = "post";
        this.paginationType = "server";			//默认分页方式是服务器分页,可选项"client"
        this.toolbarId = bstableId + "Toolbar";
        this.columns = columns;
        this.height = bgh;						//默认表格高度665
        this.data = {};
        this.mData = {};
        this.mTable = null;
        this.queryParams = {}; // 向后台传递的自定义参数


    };
    LYTable.prototype = {
        /**
         * 初始化bootstrap table
         */
        init: function (vTable) {
            var tableId = this.bstableId;
            var me = this;
            var mTvb = vTable.render({
                elem: '#' + tableId,
                url: me.url,				//请求地址
                cols: [me.columns],
                id: tableId,
                page: true,
                limit: 30,
                method: this.method,
                request: {
                    pageName: 'offset'
                },
                where: function (param) {
                    return $.extend(me.queryParams, param);
                },
                loading: true,
                height: me.height,
                limits: [10, 20, 30, 50, 70, 90, 110, 150, 200]
            });
            me.btInstance = mTvb;


            return mTvb;
        },
        initCallBack: function (vTable, someFunction) {
            var tableId = this.bstableId;
            var me = this;
            var mTvb = vTable.render({
                elem: '#' + tableId,
                url: me.url,				//请求地址
                cols: [me.columns],
                id: tableId,
                page: true,
                limit: 30,
                method: this.method,
                request: {
                    pageName: 'offset'
                },
                where: function (param) {
                    return $.extend(me.queryParams, param);
                },
                loading: true,
                height: me.height,
                limits: [10, 20, 30, 50, 70, 90, 110, 150, 200],
                done: function (res, curr, count) {
                    // me.mData=res;
                    someFunction(res)

                }
            });
            me.btInstance = mTvb;


            return mTvb;
        },
        setHeight: function (param) {
            this.height = param;
        },
        /**
         * 向后台传递的自定义参数
         * @param param
         */
        setQueryParams: function (param) {
            this.queryParams = param;
        },
        /**
         * 设置分页方式：server 或者 client
         */
        setPaginationType: function (type) {
            this.paginationType = type;
        },

        /**
         * 设置ajax post请求时候附带的参数
         */
        set: function (key, value) {
            if (typeof key == "object") {
                for (var i in key) {
                    if (typeof i == "function")
                        continue;
                    this.data[i] = key[i];
                }
            } else {
                this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
            }
            return this;
        },

        /**
         * 设置ajax post请求时候附带的参数
         */
        setData: function (data) {
            this.data = data;
            return this;
        },

        /**
         * 清空ajax post请求参数
         */
        clear: function () {
            this.data = {};
            return this;
        },

        /**
         * 刷新 bootstrap 表格
         * Refresh the remote server data,
         * you can set {silent: true} to refresh the data silently,
         * and set {url: newUrl} to change the url.
         * To supply query params specific to this request, set {query: {foo: 'bar'}}
         */
        refresh: function (parms) {
            if (typeof parms != "undefined") {
                this.btInstance.reload({
                    where: parms
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            } else {
                this.btInstance.reload({
                    where: {}
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }


        }
    };

    window.LYTable = LYTable;

}());